package com.topsmob.processer;

import com.topsmob.StartApp;
import com.topsmob.XunLeiStart;
import com.topsmob.piepline.XunLeiPiePline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;


/**
 * Created by GameLee on 2017/7/11.
 */
public class XunLeiProcesser implements PageProcessor {
    @Override
    public  synchronized void process(Page page) {
        //获取文章的
        //获得下一个翻页的ID
        boolean next = true;
        String cursor = new JsonPathSelector("$.cursor").select(page.getRawText());
        if(cursor.trim().equals("0") || cursor.trim().equals("")){
            XunLeiStart.spider.stop();
            return;
        }else{
            //获得视频列表

            List<String> videoList = new JsonPathSelector("$.list[*].video_info").selectList(page.getRawText());
            if(videoList != null){
                for(String video_str:videoList){
                    String id = new JsonPathSelector("$.id").select(video_str);
                    String title = new JsonPathSelector("$.title").select(video_str);
                    String downUrl = new JsonPathSelector("$.play_url").select(video_str);
                    //如果是已经存在则跳出循环
                    if(id.equals(XunLeiStart.lastPostId)){
                        next = false;
                        break;
                    }
                    if(XunLeiStart.isFirst){
                        //下载数据
                        XunLeiStart.isFirst = false;
                        //写入最后一个ID
                        try {
                            FileOutputStream oFile = new FileOutputStream(System.getProperty("user.dir")+ File.separator+"xunlei.properties", false);
                            XunLeiStart.properties.setProperty("lastPostId",id);
                            XunLeiStart.properties.store(oFile,"修改最后一条记录的Id");
                            oFile.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                    //执行下载
                    try {
                        XunLeiPiePline.download(downUrl,title);
                    } catch (Exception e) {
                        System.out.println("视频下载异常："+title+":"+downUrl);
                    }
                }
            }
            //查找下一页
            if(next) {
                String nextUrl = XunLeiStart.listUrl.replace("_lastId_", cursor);
                System.out.println("下一页" + nextUrl);
                 page.addTargetRequest(nextUrl);
            }
        }

    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3).setTimeOut(30*1000)
                .setDomain(XunLeiStart.domain).setCharset("utf-8");
    }
}
